﻿#encoding='utf-8'
#简单的爬虫
from database.db_handler import MysqlHander
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import os
from selenium import webdriver
import json

from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
十大股东数据
'''

entity_gupiao = "0"
entity_jijin = "2"

'''
print("000")
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")
#driver = webdriver.Chrome(chromedriver)
driver.get('http://mylib.nlc.cn/web/guest/search/searchresult?subject=40283415347ed8bd013483503a050012&singleLibList=1')
'''

#file = open('shidagudong.txt', 'w', encoding = 'utf-8')


class Gudong:
    def __init__(self, start, end):
        self.db = MysqlHander("config.ini")
        self.start = start
        self.end = end
        self.file = open('./txt/shidagudong.txt', 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

        
    def insert_gudong_gupiao(self, gupiao):
        #基金 - 股票
        sql1 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Fcreate_time, Fmodify_time) values "
        sql1 += "('" + self.jijin + "'," + entity_jijin + ",1002,'" + gupiao + "'," + entity_gupiao + ", now(), now());"
        print(sql1)
        try:
            #pass
            self.db.execute_not_safe(sql1)
        except Exception as e:
            print(str(e))
            pass
            
        #股票 - 基金
        sql2 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Fcreate_time, Fmodify_time) values "
        sql2 += "('" + gupiao + "'," + entity_gupiao + ",1001,'" + self.jijin + "'," + entity_jijin + ", now(), now());"
        print(sql2)
        try:
            #pass
            self.db.execute_not_safe(sql2)
        except Exception as e:
            print(str(e))
            pass
        #print(str(res))
    def get_shida_v1(self, code, date_str):
        self.gupaio = code
        url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112305206547387315501_1640500763433&sortColumns=HOLDER_RANK&sortTypes=1&pageSize=10&pageNumber=1&reportName=RPT_F10_EH_FREEHOLDERS&columns=ALL&source=WEB&client=WEB&filter=(SECURITY_CODE=\""+ code + "\")(END_DATE='" + date_str + "')"
        self.driver.get(url)
        start = self.driver.page_source.find("({")
        end = self.driver.page_source.find("})")
        if end > start and start > 0:
            data = self.driver.page_source[start + 1:end + 1]
            json_data = json.loads(data)
            for d in json_data["result"]["data"]:
                #print(d["HOLDER_NAME"])
                self.file.write(code + "|" + d["HOLDER_NAME"] + "|\n")
                
    def get_shida_v2(self, code, date_str):
        self.gupaio = code
        url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112305206547387315501_1640500763427&sortColumns=RANK&sortTypes=1&pageSize=10&pageNumber=1&reportName=RPT_DMSK_HOLDERS&columns=ALL&source=WEB&client=WEB&filter=(SECURITY_CODE=\"" + code + "\")(END_DATE='" + date_str + "')"
        self.driver.get(url)
        start = self.driver.page_source.find("({")
        end = self.driver.page_source.find("})")
        if end > start and start > 0:
            data = self.driver.page_source[start + 1:end + 1]
            json_data = json.loads(data)
            for d in json_data["result"]["data"]:
                #print(d["HOLDER_NAME"])
                self.file.write(code + "|" + d["HOLDER_NAME"] + "|\n")
                
    def query_codes(self):
        #sql = "select Fcode from entity_gupiao where Fid >= " + str(self.start) + " and Fid < " + str(self.end) + ";"
        sql = "select Fcode from entity_gupiao;"
        print(sql)
        self.fund_codes = self.db.query_sql(sql)
        print(self.fund_codes)
    
    def get_data(self):
        self.query_codes()
        for fund in self.fund_codes:
            print(fund)
            more = 0
            try:
                self.get_shida_v1(fund[0], "2021-12-31")
            except Exception as e:
                more = 1
                
            if more == 1:
                try:
                    self.get_shida_v1(fund[0], "2021-09-30")
                except Exception as e:
                    pass
            
            more = 0
            try:
                self.get_shida_v2(fund[0], "2021-12-31")
            except Exception as e:
                more = 1
                
            if more == 1:
                try:
                    self.get_shida_v2(fund[0], "2021-09-30")
                except Exception as e:
                    pass


if __name__ == '__main__':
    print("main")
    d = Gudong(1, 170000)
    d.get_data()
